﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Teenusekiht ;

    public class SessionData
    {

        public DateTime LastUse {get;set;}

        public int? UserId {get;set;}
        public string UserName {get;set;}
        public string UserDisp {get;set;}

        private Dictionary < int , int > cafeRoles ;

        private bool isDead ;
        private int timeOut = 20 ; //TODO: SEE PEAKS VASTAMA SESSIOONI TIMEOUT'ILE, OLEMA STATIC VÕI KONFIGURATSIOONIS!!!!!!!!!!!!!!!!!!!!!!

        public SessionData ()
	    {
            Reset () ;
	    }

        public void RemoveData ()
        {
            UserId = null ;
            UserName = null ;
            UserDisp = null ;
            Reset () ;
        }

        public void AddRoles ( KohvikuKasutajaRoll [] roles )
        {
            foreach ( KohvikuKasutajaRoll role in roles )
                cafeRoles [ role . Kohvik_ID ] = role . Roll_ID ;
        }

        public int AskRole ( int kId )
        {
            int result = -1 ;
            int value ;
            if ( cafeRoles . TryGetValue ( kId , out value ) ) result = value ;
            return ( result ) ;
        }

        public bool IsDead ()
        {
            if ( ! isDead )
                isDead = ( ( ( DateTime . Now - LastUse ) . Minutes ) > timeOut ) ? true : false ;
            return ( isDead ) ;
        }

        private void Reset ()
        {
            cafeRoles = new Dictionary < int , int > () ;
            isDead = false ;
            LastUse = DateTime . Now ;
        }

    }
